<?php

namespace Wgx\Admin\Controller;

use Wgx\Admin\Com\AdminMenuRightsUtil;
use Wgx\Admin\Com\Crud\Crud;
use Wgx\Admin\Com\Crud\CrudField;
use Wgx\Admin\Com\Crud\CrudUserInterface;
use Wgx\Admin\Com\Crud\EditPageInput\InputInput\InputEmail;
use Wgx\Admin\Com\Crud\EditPageInput\InputInput\InputTel;
use Wgx\Admin\Com\Crud\EditPageInput\InputInput\InputText;
use Wgx\Admin\Com\Crud\ListPageSearchInput\InputInput\InputNumber as LpSearchInputNumber;
use Wgx\Admin\Com\Crud\ListPageSearchInput\InputInput\InputText as LpSearchInputText;
use Wgx\Admin\Entity\AdminToMenuEntity;
use Wgx\Admin\Entity\UserEntity;
use PhpYes\Entity\BaseEntity;

class UserController extends BaseController implements CrudUserInterface
{
    public function crud()
    {
        $crud = new Crud(UserEntity::getSingle(), $this);

        $crud->addField(new CrudField('id', 'id', 'id', CrudField::ShowInAll))
            ->addField(new CrudField('createDt', '创建时间', '创建时间', CrudField::ShowInListPage | CrudField::ShowInReadPage | CrudField::ShowInExport))
            ->addField(new CrudField('nickname', '昵称', '昵称', CrudField::ShowInAll))
            ->addField(new CrudField('gender', '性别', '性别', CrudField::ShowInAll))
            ->addField(new CrudField('avatar', '头像', '头像', CrudField::ShowInAll))
            ->addField(new CrudField('phone', '手机号码', '手机号码', CrudField::ShowInAll))
            ->addField(new CrudField('email', '电子邮箱', '电子邮箱', CrudField::ShowInAll));

        $rights = AdminMenuRightsUtil::getSingleObj()->getCurrentRights();
        if ($rights && ($rights & (AdminToMenuEntity::RightRead | AdminToMenuEntity::RightUpdate | AdminToMenuEntity::RightDelete)) > 0) {
            $crud->addField(new CrudField('operate', '操作', '', CrudField::ShowInListPage));
        }

        return $crud->execute();
    }

    /**
     * @inheritDoc
     */
    public function setForOp(Crud $crud, $extra)
    {
        $op = $crud->getOp();
        $op = empty($op) ? '' : trim($op);
        if ($op == 'listpage') {
            $this->setForGetListPage($crud);
        } elseif ($op == 'editpage') {
            $this->setForGetEditPage($crud, empty($extra['entity']) ? null : $extra['entity']);
        }
    }

    public function setForGetListPage(Crud $crud)
    {
        $crud->setPageDisplayTarget(Crud::PageReadPage, 'layer', '800px', '500px');
        $crud->setPageDisplayTarget(Crud::PageEditPage, 'layer', '900px', '500px');

        // ---------- 列表页(listpage) searchbar
        $fieldName = 'nickname';
        $crudField = $crud->getField($fieldName);
        $crud->addLpSearchField(
            $fieldName,
            new LpSearchInputText(empty($crudField) ? '昵称' : $crudField->getHName(), '请输入用户昵称')
        );

        $fieldName = 'phone';
        $crudField = $crud->getField($fieldName);
        $crud->addLpSearchField(
            $fieldName,
            new LpSearchInputNumber(empty($crudField) ? '手机号码' : $crudField->getHName(), '请输入用户手机号码', null, null, 'style="width: 11em;"')
        );
        // ----------

        // ---------- 列表页(listpage) 表格的字段设置
        $crud->getField('id')->setLpWidth('20px')->setLpIsSortable(true)->setLpClassName('dt-center');
        $crud->getField('createDt')->setLpWidth('60px')->setLpIsSortable(false)->setLpClassName('dt-center');
        $crud->getField('nickname')->setLpWidth('60px')->setLpIsSortable(false)->setLpClassName('dt-center');
        $crud->getField('gender')->setLpWidth('20px')->setLpIsSortable(false)->setLpRender(
            <<<'GENDERRENDER'
function (data, type, row, meta) {
  var gender = row.gender && parseInt(row.gender) || 0;
  if (gender == 1) {
    return '男';
  } else if (gender == 2) {
    return '女';
  } else {
    return '未知';
  }
}
GENDERRENDER
        )->setLpClassName('dt-center');
        $crud->getField('avatar')->setLpWidth('60px')->setLpIsSortable(false)->setLpClassName('dt-center');
        $crud->getField('phone')->setLpWidth('60px')->setLpIsSortable(false)->setLpClassName('dt-center');
        $crud->getField('email')->setLpWidth('60px')->setLpIsSortable(false)->setLpClassName('dt-center');

        $operateField = $crud->getField('operate');
        if ($operateField) {
            $render = CrudField::getLpOperateRender($crud);
            $operateField->setLpWidth('100px')->setLpIsSortable(false)->setLpRender($render);
        }

        // ---------- 列表页(listpage) 表格的初始默认排序
        $crud->setLpOrder('id', 'desc');
    }

    /**
     * @param Crud $crud
     * @param UserEntity $entity
     */
    public function setForGetEditPage(Crud $crud, $entity)
    {
        $crud->getField('nickname')->setEpInputWidget(new InputText($entity->getNickname(), true, '请输入昵称'));
        $crud->getField('phone')->setEpInputWidget(new InputTel($entity->getPhone(), false, '请输入手机号'));
        $crud->getField('email')->setEpInputWidget(new InputEmail($entity->getNickname(), false, '请输入邮箱'));
    }

    /**
     * @inheritDoc
     */
    public function buildWhereForGetListData($params)
    {
        $where = array();

        $nickname = empty($params['nickname']) ? null : trim($params['nickname']);
        if ($nickname) {
            $where['nickname'] = array('like' => "%{$nickname}%");
        }

        $phone = empty($params['phone']) ? null : trim($params['phone']);
        if ($phone) {
            $where['phone'] = array('like' => "%{$phone}%");
        }

        return $where;
    }

    /**
     * @inheritDoc
     */
    public function buildOrderByForGetListData($fieldName, $direction)
    {
        $fieldName = empty($fieldName) ? null : trim($fieldName);
        if (empty($fieldName)) {
            return null;
        }
        $orderFieldMap = array(
            'id' => 'id',
            // 'createDt' => 'create_dt',
            // 'phone' => 'phone',
            // 'email' => 'email',
            // 'nickname' => 'nickname',
            // 'gender' => 'gender',
            // 'avatar' => 'avatar',
        );
        $fieldName = empty($orderFieldMap[$fieldName]) ? null : $orderFieldMap[$fieldName];
        if (empty($fieldName)) {
            return null;
        }

        $direction = empty($direction) ? 0 : intval($direction);
        $direction = empty($direction) ? 'desc' : 'asc';
        return array($fieldName => $direction);
    }

    /**
     * @inheritDoc
     * @param UserEntity $entity
     */
    public function formatData(Crud $crud, BaseEntity $entity, $context = null)
    {
        return $entity->formatNormal();
    }

    /**
     * @inheritDoc
     */
    public function formatListData(Crud $crud, $data, $context = null)
    {
        if (empty($data)) {
            return null;
        }

        $newDataList = array();
        foreach ($data as $item) {
            $entity = new UserEntity($item);
            $newItem = $entity->formatNormal();
            $newDataList[] = $newItem;
        }
        return $newDataList;
    }
}